package com.bw.documents.local

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.library_mvi.ui.BaseMviActivity
import com.bawei.module_documents.R
import com.bawei.module_documents.databinding.ActivityLocalBinding
import com.bawei.module_documents.local.LocalFileIntent
import com.bawei.module_documents.local.LocalFileState

import com.bw.documents.local.adapter.LocalDocumentsAdapter
import com.bw.documents.local.entity.LocalDocumentsType
import com.bw.documents.manager.adapter.DocumentsAdapter
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/documents/LocalActivity")
class LocalActivity : BaseMviActivity<ActivityLocalBinding>() {
    private lateinit var viewModel: LocalViewModel
    private lateinit var  documentsAdapter: DocumentsAdapter
    private val localAdapter : LocalDocumentsAdapter by lazy { LocalDocumentsAdapter() }

    override fun initView() {
        viewDataBinding.localPageInclude.titleTv.text = "本地文件选择"
        viewDataBinding.localPageInclude.moreTv.text = "上传"
        viewDataBinding.localPageInclude.moreTv.setOnClickListener {
            val bund = Bundle()
            localAdapter.data.forEach {
                if (it.sel){
                    bund.putString("name",it.name)
                    bund.putString("path",it.path)
                }
            }
            val intent = Intent()
            intent.putExtras(bund)
            setResult(RESULT_OK,intent)
            finish()
        }
        viewDataBinding.localPageInclude.backImg.setOnClickListener { finish() }
        val manager = LinearLayoutManager(this)
        viewDataBinding.localRv.layoutManager=manager
        localAdapter.setOnItemCLickListener{adapter,position->
            when(localAdapter.data[position].type){
                LocalDocumentsType.DOCUMENTS->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/PDFActivity").with(bund).navigation()
                }
                LocalDocumentsType.IMG->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/PhotoActivity").with(bund).navigation()
                }
                LocalDocumentsType.VIDEO->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/VideoActivity").with(bund).navigation()
                }
                LocalDocumentsType.MUSIC->{
                    val bund = Bundle()
                    bund.putString("path",localAdapter.data[position].path)
                    ARouter.getInstance().build("/documents/MusicActivity").with(bund).navigation()
                }
            }
        }
        localAdapter.setOnItemChildCLickListener{adapter,id,position->
            var flag = localAdapter.data[position].sel
            localAdapter.data.forEach{
                it.sel = false
            }
            localAdapter.data[position].sel = !flag
            localAdapter.notifyDataSetChanged()
        }
        viewDataBinding.localRv.adapter = localAdapter
    }

    override fun initData() {
        lifecycleScope.launch{
            viewModel.intents.send(LocalFileIntent.LocalFileDataIntent)
        }
    }

    override fun handleState() {
        lifecycleScope.launch{
            viewModel.states.collect{
                when(it){
                    is LocalFileState.Init ->{}
                    is LocalFileState.LocalFileSuccess ->{
                        localAdapter.setNewInstance(it.list)
                    }

                }
            }
        }
    }

    override fun getLayout(): Int {
        return R.layout.activity_local
    }

    override fun generateViewModel() {
        viewModel = ViewModelProvider(this).get(LocalViewModel::class.java)
    }


}